<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/fupan_css.css') }}" />
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>

<body>
    <div class="table-one" align="center">
        <table id="mytable" width="400px" cellspacing="0" cellpadding="0" border="0"></table>
    </div>

    <script type="text/javascript">
        var ID_THREAD = -1; //循环线程
        // 发送ajax请求，从后台获取json数据
        $(document).ready(function() {
            var codes = show_excel();
            // auto_update();
            // get_rise_data(codes)
        });

        //show the excel
        function show_excel() {
            var tb = document.getElementById("mytable"); //获取表格的dom节点
            var data = get_data();
            var row = 0;
            var data_length = data['data'].length
            var codes = new Array();
            for (var column = 0; column < data_length; column++) {
                var x = tb.insertRow(column); //插入一行
                //设置class
                if (column % 2 != 0) {
                    x.className = "cur";
                }
                var m_rows = data['data'][column];
                for (var row = 0; row < m_rows.length; row++) {
                    var y = x.insertCell(row); //插入一列
                    if (data['data'][column][row]['data'] == null && row != 0) {
                        tb.rows[column].deleteCell(row);
                        tb.rows[column].cells[row - 1].colSpan = 6;
                        break;
                    }
                    y.innerHTML = data['data'][column][row]['data'] //填充数据
                    if (row == 1) {
                        if (!isNaN(data['data'][column][row]['data'])) {
                            codes.push((data['data'][column][row]['data']).toString())
                                // console.log(data['data'][column][row]['data'])
                        }
                    }
                    //设置字体颜色
                    if (data['data'][column][row]['color'] == 'red') {
                        y.className = "red";
                    }
                    //设置背景颜色
                    if (data['data'][column][row]['background_color'] == '00fa4949') {
                        y.className = "rbnews";
                    } else if (data['data'][column][row]['background_color'] == '0049c8fa') {
                        y.className = "gbnews";
                    }
                }

            }
            return codes;
        }

        function is_running() {
            if (get_time()) {
                get_data();
            } else {
                if (ID_THREAD != -1) {
                    clearInterval(ID_THREAD)
                }
            }
        }

        function get_time() {
            //获取当前时间
            var d = new Date();
            var m_date = d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
            // return m_date;
            // 判断时间是否在开盘时间段
            if ((15 * 60 * 60) > (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) && (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) > (9 * 60 * 60 + 15 * 60)) {
                return true
            } else {
                return false
            }
        }

        function auto_update() {
            data = get_time();
            if (get_date() == data.day) {
                ID_THREAD = setInterval('is_running()', 1000 * 3); //指定3秒刷新一次
            } else {
                if (ID_THREAD != -1) {
                    clearInterval(ID_THREAD)
                }
            }
        }

        function get_date() {
            //获取当前日期
            var d = new Date();
            var m_date = d.getFullYear() + "-" + String(parseInt(d.getMonth()) + 1) + "-" + d.getDate();
            return m_date;
        }

        //kaipanla date
        function get_kpl_date() {
            var m_data = []
            $.ajax({
                url: '/test',
                data: {},
                type: 'POST',
                async: false,
                dataType: 'json',
                success: function(data) {
                    m_data = data;
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            return m_data;
        };

        // get the date from excel
        function get_data() {
            var m_data = []
            $.ajax({
                url: '/fupan_call_back',
                data: {},
                type: 'POST',
                async: false,
                dataType: 'json',
                success: function(data) {
                    // console.log(data);
                    m_data = data;
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            return m_data;
        };

        //更新表格内的数据
        function update_table(data) {
            var tb = document.getElementById("mytable"); //获取表格的dom节点
            var row = 0;
            var data_length = data.length
            var codes = new Array();
            for (var row = 1; row < tb.rows.length; row++) {
                var code = tb.rows[row].cells[1]
                if (!isNaN(code.innerHTML)) {
                    // console.log(data[code.innerHTML]['涨跌(%)'])
                    tb.rows[row].cells[6].innerHTML = data[code.innerHTML]['涨跌(%)']
                        //设置涨停背景颜色
                    if (data[code.innerHTML]['涨停价'] == data[code.innerHTML]['now'] && data[code.innerHTML]['ask1'] == 0) {
                        tb.rows[row].cells[6].className = "rbnews";
                    } else if (data[code.innerHTML]['跌停价'] == data[code.innerHTML]['now'] && data[code.innerHTML]['bid1'] == 0) {
                        tb.rows[row].cells[6].className = "gbnews";
                    }
                }
            }
        }

        // 获取股票涨幅数据
        function get_rise_data(codes) {
            var m_data = []
            $.ajax({
                url: '/codes_rise',
                data: JSON.stringify({
                    data: codes
                }),
                type: 'POST',
                async: false,
                dataType: 'json',
                contentType: 'application/json; charset=UTF-8',
                success: function(data) {
                    m_data = data;
                    update_table(data)
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            return m_data;
        };
    </script>
</body>

</html>